package com.clock.web.controller.shop;

import com.clock.common.annotation.Log;
import com.clock.common.core.controller.BaseController;
import com.clock.common.core.domain.AjaxResult;
import com.clock.common.core.page.TableDataInfo;
import com.clock.common.enums.BusinessType;
import com.clock.common.utils.poi.ExcelUtil;
import com.clock.shop.domain.ShopCoupon;
import com.clock.shop.domain.dto.UserCouponDTO;
import com.clock.shop.service.IShopCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 优惠券Controller
 * 
 * @author ruoyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/shop/coupon")
public class ShopCouponController extends BaseController
{
    @Autowired
    private IShopCouponService shopCouponService;

    /**
     * 查询优惠券列表
     */
    // @PreAuthorize("@ss.hasPermi('shop:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopCoupon shopCoupon)
    {
        startPage();
        List<ShopCoupon> list = shopCouponService.selectShopCouponList(shopCoupon);
        return getDataTable(list);
    }

    /**
     * 查询用户个人优惠券包
     */
    @GetMapping("/userCoupon")
    public TableDataInfo getUserCoupon(UserCouponDTO userCouponDTO)
    {
        startPage();
        List<ShopCoupon> list = shopCouponService.selectUserCouponList(userCouponDTO);
        return getDataTable(list);
    }

    /**
     * 查询用户在店铺能兑换的优惠券列表
     */
    @GetMapping("/canExchangeCoupon")
    public TableDataInfo getUserCanExchangeCoupon(ShopCoupon shopCoupon)
    {
        startPage();
        List<ShopCoupon> list = shopCouponService.selectUserCanExchangeCouponList(shopCoupon);
        return getDataTable(list);
    }
    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('shop:coupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopCoupon shopCoupon)
    {
        List<ShopCoupon> list = shopCouponService.selectShopCouponList(shopCoupon);
        ExcelUtil<ShopCoupon> util = new ExcelUtil<ShopCoupon>(ShopCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return success(shopCouponService.selectShopCouponByCouponId(couponId));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('shop:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopCoupon shopCoupon)
    {
        return toAjax(shopCouponService.insertShopCoupon(shopCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('shop:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopCoupon shopCoupon)
    {
        return toAjax(shopCouponService.updateShopCoupon(shopCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('shop:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(shopCouponService.deleteShopCouponByCouponIds(couponIds));
    }
}
